package com.song.user.service;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.song.user.entity.APIES36kProjectDto;
import com.song.user.entity.User;
import com.song.user.mapper.UserMapper;

import demo.Elastic.JsonUtil;

@Service
@Transactional
public class UserService {
	
	@Autowired
	UserMapper userMapper;
	@Autowired
	ElasticsearchTemplate elasticsearchTemplate;
	
	public List<User> selectPage(User user){
		List<User> list=userMapper.selectPage(user);
		
		return list;
	}
	
	
	public List<Map<String,Object>> esTest() throws Exception{
		BufferedReader br = new BufferedReader(new FileReader("e:\\all_invested_projnames.txt"));// 读取原始json文件  
        String s = null;  
        List<String> keywordsList=new ArrayList<>();
        while ((s = br.readLine()) != null) {  
             keywordsList.add(s);
        }  
        br.close(); 
		System.out.println(keywordsList.size());
		
		BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\test.txt", true)));
		
		List<Map<String,Object>> list=new ArrayList<>();
		Integer currentPage = 1;
		Integer pageSize = 5;
		for(String keywords:keywordsList){
			Map<String,Object> map=new HashMap<>();
			BoolQueryBuilder builder = QueryBuilders.boolQuery().must(QueryBuilders.multiMatchQuery(keywords, "name"));
			// 查询总数量
			NativeSearchQuery query = new NativeSearchQueryBuilder().withQuery(builder).build();
			// 查询总数量
			PageRequest qp = new PageRequest(currentPage, pageSize);
			query = new NativeSearchQueryBuilder().withQuery(builder).withPageable(qp).build();
			org.springframework.data.domain.Page<APIES36kProjectDto> page = elasticsearchTemplate.queryForPage(query, APIES36kProjectDto.class);
			List<APIES36kProjectDto> content = page.getContent();
			List<Map<String,Object>> ids=new ArrayList<>();
			for(APIES36kProjectDto obj:content){
				Map<String,Object> idmap=new HashMap<>();
				idmap.put("projectzj", obj.getProjectzj());
				ids.add(idmap);
			}
			map.put(keywords, ids);
			list.add(map);
		}
		String jsonString = JSONObject.toJSONString(list);
		System.err.println(jsonString);
		out.write(jsonString);
		out.close();
		return list;
	}
}
